package com.wykj.business.controller;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wykj.business.model.Test;
import com.wykj.business.service.HbaseService;
import com.wykj.utils.DateUtils;
import com.wykj.utils.RandomValue;

/**
 * @author cwenao
 * @version $Id HbaseAccountController.java, v 0.1 2017-02-21 22:20 cwenao Exp $$
 */
@Controller
public class HbaseController {
	private final static String TABLE_NAME = "hbase_user";

    private final static String FAMILY_INFO = "cf";

    @Autowired
    private HbaseService hbaseService;
    
    @RequestMapping(value = "/bigdata/find")
    public String findUserInfoByName(String name, ModelMap modelMap) {
        Test test = hbaseService.get(TABLE_NAME, "2");
        
        //hbaseService.put(TABLE_NAME, "2", FAMILY_INFO, "1", "xxx");

        modelMap.addAttribute("test", test);

        return "hbase/hbasetest";
    }
    
    @RequestMapping(value = "/bigdata/reloadData")
    public @ResponseBody String reloadData(String name, ModelMap modelMap) {

    	int i = 30;
		while(i>0){
			String row = UUID.randomUUID().toString();
			String date = DateUtils.getDateTimeFormat(new Date());
			Map map = RandomValue.getAddress();
			hbaseService.put("H_U", row, "COL", "PHONE", map.get("tel").toString());
			hbaseService.put("H_U", row, "COL", "GENDER", map.get("sex").toString());
			hbaseService.put("H_U", row, "COL", "ISLOGIN", "true");
			hbaseService.put("H_U", row, "COL", "TYPE", "3");
			hbaseService.put("H_U", row, "COL", "DELFLAG", "0");
			hbaseService.put("H_U", row, "COL", "LONGITUDE", map.get("longitude").toString());
			hbaseService.put("H_U", row, "COL", "LATITUDE", map.get("latitude").toString());
			hbaseService.put("H_U", row, "COL", "DATE", date);
			hbaseService.put("H_U", row, "COL", "INFO", "随机数据");
			//String phoenixSQL = "upsert into H_U (ROW,PHONE,GENDER,ISLOGIN,TYPE,DELFLAG,LONGITUDE,LATITUDE,DATE,INFO) values ('"+row+"','"+map.get("tel")+"','"+map.get("sex")+"','true','3','0','"+map.get("longitude")+"','"+map.get("latitude")+"','"+date+"','随机数据');";
			//String phoenixSQL = "upsert into H_U values ('"+row+"','"+map.get("tel")+"','"+map.get("sex")+"','true','3','0','"+map.get("longitude")+"','"+map.get("latitude")+"','"+date+"','随机数据') ";
			//int res = PhoenixClient.execUpd(phoenixSQL);
			i--;
		}
		
		int j = 30;
		while(j>0){
			String row = UUID.randomUUID().toString();
			String date = DateUtils.getDateTimeFormat(new Date());
			Map map = RandomValue.getAddress();
			hbaseService.put("H_G", row, "COL", "IP", map.get("ip").toString());
			hbaseService.put("H_G", row, "COL", "PHONEMODE", map.get("mobileModel").toString());
			hbaseService.put("H_G", row, "COL", "LONGITUDE", map.get("longitude").toString());
			hbaseService.put("H_G", row, "COL", "LATITUDE", map.get("latitude").toString());
			hbaseService.put("H_G", row, "COL", "DATE", date);
			hbaseService.put("H_G", row, "COL", "INFO", "随机数据");
			j--;
		}
		
        hbaseService.put(TABLE_NAME, "2", FAMILY_INFO, "1", "xxx");

        return "hbase/hbasetest";
    }

}
